const app = getApp();
Page({
  data:{
    title:'相关条款',
    name :'',
    phoneNumber :'',
    identityCard :'',
    bankName:'',
    bankId:'',
    isdisabled:false,
    loading:false
    // cardNumber:'621799121001442575'
  },
  onShow:function(){
    const _this = this;
    wx.getStorage({
      key: 'banks',
      success: function (res) {
        _this.setData({
          bankName: res.data.bankName,
          bankId: res.data.bankId
        })
        console.log(res.data);
      }
    })
    wx.removeStorage({
      key: 'banks',
      success: function (res) {
        console.log(res.data)
      }
    })
    this.getInfo();
  },
  onLoad:function(options){
    const _this = this;
    // console.log(options.bankName);
    // if (options.bankName) {
    //   this.setData({
    //     bankName: options.bankName
    //   })
    // }
    if(options.phone){
      this.setData({
        phoneNumber: options.phone,
        name: options.name,
        identityCard: options.idnumber,
      })
    }
  },
  //用户信息
  getInfo() {
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetCurrentUserInfo',
      data: {},
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          _this.setData({
            name: res.data.result.name,
            phoneNumber: res.data.result.phoneNumber,
            identityCard: res.data.result.identityCard,
          })
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  getname:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  getidnum: function (e) {
    this.setData({
      identityCard: e.detail.value
    })
  },
  // 自动匹配银行卡
  getBankNumber: function (e) {
    console.log(e.detail.value);
    const _this = this;
    _this.setData({
      cardNumber: e.detail.value
    })
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetBankShortName',
      data: {
        _input_charset: 'utf-8',
        cardNo: e.detail.value
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.success){
          if(res.data.result){
            if (res.data.result.validated) {
              _this.setData({
                id: res.data.result.bank,
                isdisabled:false
              })
              _this.getBankList(_this.data.id);
            } else {             
              _this.setData({
                id: ''
              })
            }
            if(!res.data.result.bank){
              console.log("不存在")
              wx.showToast({
                title: '暂不支持该行，请更换银行卡！',
                icon: 'none',
                duration: 2000
              })
              _this.setData({
                isdisabled:true
              })
            }
          }
        }else{
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete:function(res){
        // wx.showToast({
        //   title: '1111111111111111111111111',
        //   icon: 'success',
        //   duration: 2000
        // })
      }
    })
  },
  // 银行卡信息获得
  getBankList:function(id){
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetBankList',
      data: {
        bankCode:id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if(res.data.success){
            if (res.data.result.items.length > 1) {
              _this.setData({
                bankName: '',
                bankId: ''
              })
            } else if (res.data.result.items.length == 1) {
              _this.setData({
                bankName: res.data.result.items[0].bankName,
                bankId: res.data.result.items[0].id
              })
            } else if (res.data.result.items.length == 0){
            wx.showToast({
              title: '暂不支持该行，请更换银行卡！',
              icon: 'none',
              duration: 2000
            })
            _this.setData({
              isdisabled: true
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 验证身份证
  // isCardNoTrue:function(card) { 
  //   // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
  //   var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
  //   if(reg.test(card) === false) {
  //     wx.showModal({
  //       content: '身份证格式不正确',
  //       showCancel: false,
  //       success: function (res) {
  //         if (res.confirm) {
  //           console.log('用户点击确定')
  //         }
  //       }
  //     });
  //     return false;
  //   }
  //   return true;
  // },
  // 下一步
  formSubmit:function(e){
    const _this = this;
    // console.log(e);
    if (_this.data.bankId && e.detail.value.cardNumber){
      // if(this.isCardNoTrue(e.detail.value.idNumber)){
        _this.setData({
          loading:true,
          isdisabled:true
        })
        wx.request({
          url: app.globalData.path + 'services/app/applets/BindBankCard',
          data: {
            bankId: _this.data.bankId,
            identityCard: _this.data.identityCard,
            bankCard: e.detail.value.cardNumber,
            accountName: _this.data.name
          },
          method: 'POST',
          header: {
            'content-type': 'application/json', // 默认值
            'Authorization': 'Bearer ' + app.globalData.authorization
          },
          success: function (res) {
            console.log(res.data)
            if (res.data.success) {
              _this.setData({
                loading: false,
                isdisabled: false
              })
              wx.reLaunch({
                url: '../paypwd/paypwd?phone=' + e.detail.value.phone
              })
            } else {
              _this.setData({
                loading: false,
                isdisabled: false
              })
              if(res.data.error.code == -1){
                wx.showToast({
                  title: '请添加与员工信息一致的银行卡',
                  icon: 'none',
                  duration: 2000
                })
              }else{
                wx.showToast({
                  title: res.data.error.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
            // else{
            //   wx.showModal({
            //     content: res.data.error.message,
            //     showCancel: false,
            //     success: function (res) {
            //       if (res.confirm) {
            //         console.log('用户点击确定')
            //       }
            //     }
            //   });
            // }
          }
        })
      // }
    }else{
      wx.showModal({
        content:'请填写银行卡号或选择发行银行',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
    }
  },
  toBanks: function () {
    const _this = this;
    wx.navigateTo({
      url: '../banks/banks?from=add',
    })
  }
})